package com.wing.order.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.order.model.response.InventoryInoutJobVO;
import com.wing.order.model.response.InventoryInoutVO;
import com.wing.order.service.InventoryInoutJobService;
import com.wing.order.service.InventoryInoutService;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.response.ProductDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-入库单计划管理", tags = "入库单计划管理")
@RestController("appInventoryInoutController")
@RequestMapping("order/app/inventoryInout")
@RequiredArgsConstructor
public class InventoryInoutController {


    private final InventoryInoutService inventoryInoutService;
    private final InventoryInoutJobService inventoryInoutJobService;
    private final EnterpriseClient enterpriseClient;

    @ApiOperation(value = "查询所有入库单", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "status", value="状态",  dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutVO> list(
            @RequestParam( defaultValue = "1", required = false) Integer page
            ,@RequestParam( defaultValue = "10", required = false) Integer limit
            ,@RequestParam( defaultValue = "create_time", required = false) String sort
            ,@RequestParam( defaultValue = "desc", required = false) String order
            , String status //stockup(0,待入库);storagein(1,入库中);storage(2,入库完成)
    ) {
        Page<InventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(status)) {
            params.put("status", status);
        }

        Page<InventoryInoutVO> list = inventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "入库作业单", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/jobList")
    public PageResult<InventoryInoutJobVO> jobList (
            @RequestParam( defaultValue = "1", required = false) Integer page
            ,@RequestParam( defaultValue = "10", required = false) Integer limit
            ,@RequestParam( defaultValue = "create_time", required = false) String sort
            ,@RequestParam( defaultValue = "desc", required = false) String order
    ){
        Page<InventoryInoutJobVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        Page<InventoryInoutJobVO> list  = inventoryInoutJobService.pageList4app(pg,params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    //入库作业单sn码明细

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "planId", value="入库计划id",  dataType = "Long", paramType = "query")
    })
    @ApiOperation(value = "条码详情", notes = "搜索")
    @GetMapping("/lableList")
    public PageResult <ProductPackLabelVO> lableList(@RequestParam(value ="page",defaultValue = "1") Integer page
            , @RequestParam(value = "limit",defaultValue = "10") Integer limit
            , @RequestParam(value ="sort",defaultValue = "create_time") String sort
            , @RequestParam(value ="order",defaultValue = "asc") String order
            , @RequestParam(value ="planId",required = false) Long planId){
        return enterpriseClient.getInventoryPackLabelPageList("",page,limit,sort,order,planId);

    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code",  dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "打印条码", notes = "搜索")
    @GetMapping("/getBarCode")
    public JsonResult<String> getBarCode(@RequestParam(value = "code") String code){
        return enterpriseClient.getBarCode(code);
    }

    //在库产品列表
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "在库产品列表", notes = "搜索")
    @GetMapping("/getInventoryProductList")
    public PageResult<ProductDetailVO> getInventoryProductList(@RequestParam(value ="page",defaultValue = "1") Integer page
            , @RequestParam(value = "limit",defaultValue = "10") Integer limit
            , @RequestParam(value ="sort",defaultValue = "create_time") String sort
            , @RequestParam(value ="order",defaultValue = "asc") String order
            , String  query
            , ProductCategory.Type type
            , Long productTypeId
            , Long brandId
            , Long supplierId
            , ProductCategory.CategoryType categoryType
    ){
        Page<ProductDetailVO> pg = new Page<>(page, limit);
        Map params=new HashMap();
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(query)){
            params.put("query",query);
        }
        if (null != type){
            params.put("type",type.getValue());
        }
        if (null != productTypeId){
            params.put("productTypeId",productTypeId);
        }
        if (null != brandId){
            params.put("brandId",brandId);
        }
        if (null != supplierId){
            params.put("supplierId",supplierId);
        }
        if(null != categoryType){
            params.put("categoryType", categoryType.getValue());
        }else{
            params.put("categoryType", ProductCategory.CategoryType.purchase);
        }
        IPage<ProductDetailVO> list = inventoryInoutService.getProductList(pg,params);
        return new PageResult<>(list.getRecords(), list.getTotal());

    }

    //产品的库存分布  分布在哪些仓库
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "skuCode",  dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "产品的库存分布", notes = "搜索")
    @GetMapping("/getProductWarehouseList")
    public JsonResult getProductWarehouseList(@RequestParam(value ="skuCode")String skuCode){
        List<Long> list = inventoryInoutService.findWarehouseId(skuCode);//查询有该产品在的所有仓库的id
        List productWarehouseList = new ArrayList();
        for (int i = 0;i<list.size();i++){
            List<Map> info = inventoryInoutService.findWarehouseInforById(list.get(i),skuCode);
            for (int j = 0;j<info.size();j++){
                info.get(j).put("warehouseId",list.get(i));
                info.get(j).put("skuCode",skuCode);
            }
            productWarehouseList.addAll(info);
        }
        return JsonResult.success("查询成功",productWarehouseList);
    }

    //产品的料箱分布
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "skuCode",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "id", value = "仓库id", dataType = "Long", paramType = "query")
    })
    @ApiOperation(value = "产品的料箱分布", notes = "搜索")
    @GetMapping("/getProductBoxList")
    public JsonResult getProductBoxList(@RequestParam(value ="skuCode")String skuCode,@RequestParam(value ="id")Long id){//仓库id
        List<Map> list = inventoryInoutService.queryProductBoxList(skuCode,id);
        return JsonResult.success("查询成功",list);
    }
}
